var PopBox = new Class({
  klass: 'PopBox',
  
  initialize: function(insides){
    this.element = html.DIV({'class':'popbox-container'}, insides);
  },
  
  // Holds an HTML element that will be inserted into the documents
  element: null
});

// Static class to manage popups
var PopManager = {
  
  // PUBLIC VARIABLES
  
  // This defines the starting z-index for the popups
  startZ: 101,
  
  
  // PRIVATE VARIABLES
  
  activeBoxes: [],
  modalized: false,
  
  // PUBLIC METHODS
  
  // Input expects a type PopBox
  createPop: function(popbox, modal){
    // Default values
    modal = modal == null ? true : false;
    
    // Ensure we are getting the right type of object
    if (typeof popbox != 'object' || popbox.klass != 'PopBox') return;
    
    // Keep track of internal functions
    this.activeBoxes.push(popbox);
    if (modal) this._createOverlay();
    
    // Enable escape-key closing
    this.enableKeys();
    
    // Insert the new popbox
    var el = $(popbox.element);
    //var zindex = this.activeBoxes.length > 0 ? this.activeBoxes.getLast().element.getStyle('z-index') : this.startZ;
    // piece of ass safari. I hate you so much
    el.setOpacity(0);
    var zindex = this.startZ;
    zindex = zindex == "auto" ? this.startZ + 1 : zindex++;
    el.setStyle('z-index', zindex);
    el.injectInside(document.body);
    new Fx.Style(el, 'opacity', {duration:1000}).start(1);
    
    this._center(this.activeBoxes.getLast().element);
  },
  
  removePop: function(popbox){
    
    // Ensure we are getting the right type of object
    if (typeof popbox != 'object' || popbox == null || popbox.klass != 'PopBox') return;
    
    // Remove the popbox
    popbox.element.remove();
    
    // Keep track of internal functions
    this.activeBoxes.remove(popbox);
    if (this.modalized && this.activeBoxes.length == 0){
      this._removeOverlay();
      this.disableKeys();
    }
  },
  
  removeLast: function(){
    if (this.activeBoxes.length < 1) return;
    this.removePop(this.activeBoxes.getLast());
  },
  
  enableKeys: function(){
    $(document).addEvent('keypress', this._kbdInput.bindAsEventListener(this));
  },
  
  disableKeys: function(){
    $(document).removeEvent('keypress', this._kbdInput.bindAsEventListener(this));
  },
  
  // PRIVATE METHODS
  
  _kbdInput: function(e){
    var ev = new Event(e);
    if (ev.key == 'esc') this.removePop(this.activeBoxes.getLast());
  },
  
  _createOverlay: function(){
    // Build the overlay
    var iframe = $(html.IFRAME({id:'popbox_iframe', 'class':'popbox-iframe'}));
    iframe.injectInside(document.body);
    var overlay = $(html.DIV({id:'popbox_overlay', 'class':'popbox-overlay'}));
    overlay.injectInside(document.body);
    this._resizeOverlay();
    
    // Keep track of internal functions
    this.modalized = true;
  },
  
  _removeOverlay: function(){
    $('popbox_overlay').remove();
    $('popbox_iframe').remove();
    
    // Keep track of internal functions
    this.modalized = false;
  },
  
  _resizeOverlay: function(){
    $('popbox_overlay').setStyles({"height": window.getScrollHeight()+'px', "width": window.getWidth()+'px'});
    $('popbox_iframe').setStyles({"height": window.getScrollHeight()+'px', "width": window.getWidth()+'px'});
  },
  
  _center: function(el){
    var wdims = window.getSize();
    var bdims = el.getSize();
    var topOffset = 0;
    if (wdims.size.y < bdims.size.y){
      // Box is taller than viewable window
      topOffset = 50;
    }else{
      // Box is shorter than viewbale window
      var diff = wdims.size.y - bdims.size.y;
      topOffset = (diff/2).toInt();
    }
    topOffset = wdims.scroll.y + topOffset;
    
    el.setStyle('position', 'absolute');
    el.setStyle('top', topOffset + 'px');
    el.setStyle('left', '50%');
    var width = el.getStyle('width').toInt() + el.getStyle('padding-left').toInt() + el.getStyle('padding-right').toInt();
    el.setStyle('margin-left', (-1*width/2).toInt());
  }
}